/**** COMMON ****/
var b_gaid="UA-12420642-2";
if(!$tt) {
	//attempt to load ga library
	try{
		if(!_gaq) {
			var $B_gaq = _gaq || [];
			$B_gaq.push(['_setAccount', b_gaid]);
			
			(function() {
				var $B_ga = document.createElement('script'); $B_ga.type = 'text/javascript'; $B_ga.async = true;
				$B_ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
				var $B_s = document.getElementsByTagName('script')[0]; $B_s.parentNode.insertBefore($B_ga, $B_s);
			})();
		} else {
			var $B_gaq = _gaq;
		}
	} catch(e) { /*ignore*/ }
	
	if(!$tt){
		if(turntable){ var $tt=turntable; }//grab turntable object
		if($tt){
			for(i in $tt){
				var $tt_roomkey=$tt[i]; break; //grab roomkey object
			}
		}
		if($tt_roomkey){
			for(i in $tt_roomkey){
				if($tt_roomkey[i]&&$tt_roomkey[i].callback) {
					var $tt_vote=$tt_roomkey[i]; break; //grab object for callback
				}
			}
		}
	}
}
if(!autoStatusDiv) {
	var autoStatusDiv=$('#bcj19-tt').html();
	if(!autoStatusDiv) {
		//$('div.header').append('<div id="bcj19-tt"><table><tr id="bcj19-tt-tr"><td id="bcj19-tt-awesome" title="Auto-Awesome">&nbsp;</td><td id="bcj19-tt-dj" title="Auto-DJ">&nbsp;</td><td id="bcj19-tt-skip" title="Auto-Skip">&nbsp;</td></tr></table>');
		$('div.header-content').append('<div id="bcj19-tt"><table><tr id="bcj19-tt-tr"><td id="bcj19-tt-awesome" title="Auto-Awesome">&nbsp;</td><td id="bcj19-tt-dj" title="Auto-DJ">&nbsp;</td><td id="bcj19-tt-skip" title="Auto-Skip">&nbsp;</td></tr></table>');
	}
}
/**** END COMMON ****/

/*
	function countVotes() - if all possible voters have voted, skip the rest of the song
	- pv_userCount = count of total users in room, including current dj
	- pv_redo = true|false|null - if false, cancel the timeout loop
*/
function countVotes(pv_redo) {
	var userCount=0; 
	var upvoterCount=0;
	var songName;
	var myDjId=$tt_vote.myuserid;
	var currentDjId=$tt_roomkey.currentDj;
	
	//identify current dj - is it me?  if so, do the following, otherwise, skip it...
	if(myDjId==currentDjId) {
		for(i in $tt_roomkey.users) { userCount=userCount+1; } //count of total users in room, including current dj
		for(ii in $tt_roomkey.upvoters) { upvoterCount=upvoterCount+1; /*console.log(upvoterCount);*/ }
		
		try{ songName=$tt_roomkey.currentSong.metadata.song; } catch(e) { songName="song name unavailable"; }
		
		if(upvoterCount>=userCount-1&&userCount>1) { 
			$tt_vote.callback('stop_song');
			console.log('skipped:  ' + songName);
		}
	}
	
	if(pv_redo) { var tSkip=setTimeout('countVotes(ttSkipEnabled)',500); } //
	else { clearTimeout(tSkip); }
}

//toggle whether tt-skip is enabled or disabled and send call to ga
if(ttSkipEnabled) { 
	ttSkipEnabled=false; 
	$('#bcj19-tt-skip').css('background-color','');
	$('#bcj19-tt-skip').attr('title','Auto-Skip Disabled');
	
	//send ga call
	try{
		$B_gaq.push(['_setAccount', b_gaid])
		$B_gaq.push(['_trackEvent','Auto-Skip','Disabled']);
		
		$B_gaq.push(['_setAccount', 'UA-20455078-4']);
	} catch(e) { /*ignore*/ }
} else { 
	var ttSkipEnabled=true;
	$('#bcj19-tt-skip').css('background-color','#C0C0C0');
	$('#bcj19-tt-skip').attr('title','Auto-Skip Enabled');
	
	//send ga call
	try{
		$B_gaq.push(['_setAccount', b_gaid]);
		$B_gaq.push(['_trackEvent','Auto-Skip','Enabled']);
		
		$B_gaq.push(['_setAccount', 'UA-20455078-4']);
	} catch(e) { /*ignore*/ }
}

try {
	countVotes(ttSkipEnabled);
} catch(e) { /*ignore*/ }